package com.max.common.oauth.oauth10;

import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.max.common.util.XMLConfig;

public class Config
{
	private static final String OAUTH10_FILE_NAME = "oauth10.xml";
	
	private static Map<String,OAuthProvider> providerMap = null;
	
	private static Map<String,OAuthConsumer> consumerMap = null;
	
	private static Config instance = null;
	
	private Config()
	{
	}
	
	public static synchronized Config getInstance()
	{
		if (instance == null)
		{
			instance = new Config();
			
			try 
			{
				URL url = Config.class.getClassLoader().getResource(OAUTH10_FILE_NAME);
				XMLConfig config = XMLConfig.getInstance(new File(url.getFile()));
		        
				/**prase provider*/
		        Map<String,Map<String,String>> pmap = config.getAllValues("oauth/provider", "name");
		        for (Iterator<Entry<String, Map<String, String>>> iter = pmap.entrySet().iterator(); iter.hasNext();)
		        {
		        	 Entry<String,Map<String,String>> entry = iter.next();
		        	 String name = entry.getValue().get("name");
		        	 String method = entry.getValue().get("method");
		        	 String requestTokenURL = entry.getValue().get("requestTokenURL");
		        	 String authorizationURL = entry.getValue().get("authorizationURL");
		        	 String accessTokenURL = entry.getValue().get("accessTokenURL");
		        	 
		        	 OAuthProvider provider = new OAuthProvider(
		        			 method, requestTokenURL, authorizationURL, accessTokenURL);
		        	 
		        	 if(providerMap == null)
		        		 providerMap = new HashMap<String,OAuthProvider>(4);
		        	 providerMap.put(name, provider);
		        }
		        
		        
		        /**prase consumer*/
		        Map<String,Map<String,String>> cmap = config.getAllValues("oauth/consumer", "name");
		        for (Iterator<Entry<String, Map<String, String>>> iter = cmap.entrySet().iterator(); iter.hasNext();)
		        {
		        	 Entry<String,Map<String,String>> entry = iter.next();
		        	 String name = entry.getValue().get("name");
		        	 String consumer_key = entry.getValue().get("consumer_key");
		        	 String consumer_secret = entry.getValue().get("consumer_secret");
		        	 String provider = entry.getValue().get("provider");
		        	 
		        	 OAuthConsumer consumer = new OAuthConsumer(consumer_key, 
		        			 consumer_secret, providerMap==null?null:providerMap.get(provider));
		        	 
		        	 if(consumerMap == null)
		        		 consumerMap = new HashMap<String,OAuthConsumer>(4);
		        	 consumerMap.put(name, consumer);
		        }
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		}
		
		return instance;
	}
	
	
	public static Map<String, OAuthConsumer> getConsumerMap() 
	{
		getInstance();
		return consumerMap;
	}

	public static Map<String, OAuthProvider> getProviderMap() 
	{
		getInstance();
		return providerMap;
	}
	
	public static void main(String [] args)
	{
		Config.getInstance();
	}

}
